#!/bin/sh

#log script, by john stultz (jstultz@us.ibm.com)
# other bits by darrick wong (djwong@us.ibm.com)

# Copyright (C) 2003-2006 IBM
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.


trap 'exit 0' 15

function startup() {
	# change into the pounder log dir
	if [ -x $POUNDER_LOGDIR ]; then
		cd $POUNDER_LOGDIR
	fi
	#create log dir
	mkdir statlogs
	cd statlogs
}


INFOFILE=info
#generic system info
function sysinfo() {
	uname -a >> $INFOFILE
	printf "\n[cpuinfo]=================\n" >>$INFOFILE
	cat /proc/cpuinfo >> $INFOFILE
	printf "\n[meminfo]=================\n" >>$INFOFILE
	cat /proc/meminfo >> $INFOFILE
	printf "\n[ifinfo]=================\n" >>$INFOFILE
	/sbin/ifconfig >> $INFOFILE
	printf "\n[sysctl]=================\n" >>$INFOFILE
	sysctl -a >> $INFOFILE
}

PROC_ENTRIES="buddyinfo diskstats meminfo slabinfo net/netstat net/snmp"
function procinfo() {
	#get a timestamp
	NOW=`date`
	for i in $PROC_ENTRIES
	do
		logfile=`basename $i`
		printf "\n$NOW\n" >> $logfile.log
		cat /proc/$i >> $logfile.log
		sleep 1
	done
}

# ten second delay; run 150s before outputting timestamp
DELAY=10
COUNT=15

#single shot logging apps
function singleshots(){
	echo > vmstat.log
	echo > iostat.log

	while true; do
		vmstat $DELAY $COUNT >> vmstat.log
		NOW=`date`
		printf "\n$NOW\n\n" >> vmstat.log
	done &

	IOSTAT=`which iostat 2> /dev/null`
	if [ -n "$IOSTAT" -a -x "$IOSTAT" ]; then
		while true; do
			iostat -x $DELAY $COUNT >> iostat.log
			NOW=`date`
			printf "\n$NOW\n\n" >> iostat.log
		done &
	fi
}

# periodically run apps & functions
function runlogging() {
	CMDS="procinfo"
	while true
	do
		for i in $CMDS
		do
			$i
			sleep 1
		done
		sleep $DELAY
	done
}

trap 'exit 0' 15

startup $*
sysinfo
singleshots
tail -f /var/log/messages /var/log/syslog /var/log/daemon.log /var/log/kern.log /var/log/warn /var/log/faillog > system_logs &
cp -pRdu $POUNDER_HOME/README .
runlogging

exit 0
